<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./lib/jquery-1.11.3/jquery.js"></script>
    <style>
        #wai1 {
            position: relative;
            z-index: 10;
            height: 410px;
            width: 410px;
            border: 1px solid #000;
        }

        #wai1 img {
            width: 410px;
        }

        #wai {
            position: absolute;
            top: 0;
            left: 0;
            z-index: 1000;
            height: 360px;
            width: 360px;
            margin: 25px;
        }

        #box {
            position: absolute;
            top: 0;
            left: 420px;
            width: 250px;
            height: 250px;
            border: 1px solid #000;
            display: none;
            background: url('./images/11.jpg') no-repeat;
            /* 放大镜放大图片五倍410*5 */
            background-size: 2050px
        }
    </style>
</head>

<body>
    <div id="wai1">
        <img src="./images/11.jpg" alt="">
        <div id="wai">
        </div>
        <div id="box"></div>
    </div>
    <script>
        let wai = document.getElementById("wai");
        console.log(wai);
        // 鼠标移动事件
        $('#wai').on("mousemove", function (e) {
            // console.log(e.offsetX, e.offsetY);
            // 获取鼠标移动到图片指定位置将其坐标获取并将其放大5倍，用来在放大镜div里作为背景定位坐标
            let x = e.offsetX * 5;
            let y = e.offsetY * 5;
            // 获取并修改后坐标渲染给放大镜div
            $('#box').css({
                backgroundPositionX: -x,
                backgroundPositionY: -y,
                display: "block"
            })
            // `margin-top: ${e.offsetY-25}px;margin-left: ${e.offsetX-25}px;`;
        });
        // 鼠标离开盒子隐藏放大镜盒子
        $('#wai').on("mouseleave", function () {
            $('#box').css({
                display: "none"
            })
        })
    </script>

</body>

</html>